package P1线性结构.LeetCode单调栈;

import P1线性结构.LinkedList;

public class LeetCode739 {
    public int[] dailyTemperatures(int[] temperatures) {
        int[] ans = new int[temperatures.length];
        //单调递减栈
        LinkedList<Integer> stack = new LinkedList<>();
        for (int i = 0; i < temperatures.length; i++) {
            int temperature = temperatures[i];
            while (!stack.isEmpty() && temperatures[stack.peek()] < temperature) {
                int preIndex = stack.pop();
                ans[preIndex] = i - preIndex;
            }
            //当栈位空 或者 温度小于等于栈顶 进栈
            stack.push(i);
        }
        return ans;
    }
}
